﻿using System;
using System.Collections.Generic;
using System.Text;
using RealState.BusinessObjects;
using System.Data;
using System.Data.Common;

namespace RealState.DAL
{
    public class VwContentDAO
    {
        public VwContentDAO()
        {
            DbProviderHelper.GetConnection();
        }
        public List<VwContents> GetViewContent(VwContents vcontent)
        {
            long priceMin = 0;
            long priceMax = 0;
            bool isAuction = false;
            getPriceRange(vcontent.Price, ref priceMin, ref priceMax, ref isAuction);
            try
            {
                List<VwContents> lstContentss = new List<VwContents>();
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECTVw_Contents_2", CommandType.StoredProcedure);
                if (!string.IsNullOrEmpty(vcontent.Title))
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Title", DbType.String, vcontent.Title));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Title", DbType.String, DBNull.Value));
                if (!string.IsNullOrEmpty(vcontent.TradeType))
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TradeType", DbType.String, vcontent.TradeType));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TradeType", DbType.String, DBNull.Value));
                if (vcontent.CityId != 0)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CityId", DbType.Int32, vcontent.CityId));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CityId", DbType.Int32, DBNull.Value));
                if (vcontent.DistrictID != 0)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@DistrictID", DbType.Int32, vcontent.DistrictID));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@DistrictID", DbType.Int32, DBNull.Value));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@PriceMin", DbType.Int64, priceMin));
                if (priceMax != 0)
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@PriceMAX", DbType.Int64, priceMax));
                else
                    oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@PriceMAX", DbType.Int64, DBNull.Value));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@IsAuction", DbType.Boolean, isAuction));
                DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
                while (oDbDataReader.Read())
                {
                    VwContents oContents = new VwContents();
                    oContents.Id = Convert.ToInt32(oDbDataReader["Id"]);
                    oContents.Title = Convert.ToString(oDbDataReader["Title"]);
                    oContents.Code = Convert.ToString(oDbDataReader["Code"]);
                    oContents.CreatedTime = Convert.ToDateTime(oDbDataReader["CreatedTime"]).ToString("dd/MM/yyyy");
                    if (oDbDataReader["ImgPath"] != DBNull.Value)
                        oContents.ImgPath = Convert.ToString(oDbDataReader["ImgPath"]);
                    if (oDbDataReader["CategoryId"] != DBNull.Value)
                        oContents.CategoryId = Convert.ToInt32(oDbDataReader["CategoryId"]);
                    if (oDbDataReader["Price"] != DBNull.Value)
                        oContents.Price = Convert.ToInt64(oDbDataReader["Price"]);
                    if (oDbDataReader["TradeType"] != DBNull.Value)
                        oContents.TradeType = Convert.ToString(oDbDataReader["TradeType"]);
                    if (oDbDataReader["CityName"] != DBNull.Value)
                        oContents.CityName = Convert.ToString(oDbDataReader["CityName"]);
                    if (oDbDataReader["DistrictName"] != DBNull.Value)
                        oContents.DistrictName = Convert.ToString(oDbDataReader["DistrictName"]);
                    if (oDbDataReader["CategoryName"] != DBNull.Value)
                        oContents.CategoryName = Convert.ToString(oDbDataReader["CategoryName"]);
                    if (oDbDataReader["CompanyName"] != DBNull.Value)
                        oContents.CompanyName = Convert.ToString(oDbDataReader["CompanyName"]);
                    if (oDbDataReader["CompanyAddress"] != DBNull.Value)
                        oContents.CompanyAddress = Convert.ToString(oDbDataReader["CompanyAddress"]);
                    if (oDbDataReader["CompanyTel"] != DBNull.Value)
                        oContents.CompanyTel = Convert.ToString(oDbDataReader["CompanyTel"]);
                    if (oDbDataReader["CompanyFax"] != DBNull.Value)
                        oContents.CompanyFax = Convert.ToString(oDbDataReader["CompanyFax"]);
                    if (oDbDataReader["CompanyEmail"] != DBNull.Value)
                        oContents.CompanyEmail = Convert.ToString(oDbDataReader["CompanyEmail"]);
                    if (oDbDataReader["Website"] != DBNull.Value)
                        oContents.Website = Convert.ToString(oDbDataReader["Website"]);
                    if (oDbDataReader["BusinessOwner"] != DBNull.Value)
                        oContents.BusinessOwner = Convert.ToString(oDbDataReader["BusinessOwner"]);
                    if (oDbDataReader["TaxCode"] != DBNull.Value)
                        oContents.TaxCode = Convert.ToString(oDbDataReader["TaxCode"]);
                    if (oDbDataReader["StartTime"] != DBNull.Value)
                        oContents.StartTime = Convert.ToDateTime(oDbDataReader["StartTime"]).ToString("dd/MM/yyyy");
                    if (oDbDataReader["Status"] != DBNull.Value)
                        oContents.Status = Convert.ToString(oDbDataReader["Status"]);
                    if (oDbDataReader["Revenue"] != DBNull.Value)
                        oContents.Revenue = Convert.ToString(oDbDataReader["Revenue"]);
                    if (oDbDataReader["LastRevenue"] != DBNull.Value)
                        oContents.LastRevenue = Convert.ToString(oDbDataReader["LastRevenue"]);
                    if (oDbDataReader["AnnualProfit"] != DBNull.Value)
                        oContents.AnnualProfit = Convert.ToString(oDbDataReader["AnnualProfit"]);
                    if (oDbDataReader["LastAnualProfit"] != DBNull.Value)
                        oContents.LastAnualProfit = Convert.ToString(oDbDataReader["LastAnualProfit"]);
                    if (oDbDataReader["Assets"] != DBNull.Value)
                        oContents.Assets = Convert.ToString(oDbDataReader["Assets"]);
                    if (oDbDataReader["Reason"] != DBNull.Value)
                        oContents.Reason = Convert.ToString(oDbDataReader["Reason"]);
                    if (oDbDataReader["SaleType"] != DBNull.Value)
                        oContents.SaleType = Convert.ToString(oDbDataReader["SaleType"]);
                    if (oDbDataReader["IsAuction"] != DBNull.Value)
                        oContents.Auction = (Convert.ToBoolean(oDbDataReader["IsAuction"]) == true) ? "Có" : "Không";
                    if (oDbDataReader["ProjectTitle"] != DBNull.Value)
                        oContents.ProjectTitle = Convert.ToString(oDbDataReader["ProjectTitle"]);
                    if (oDbDataReader["Investor"] != DBNull.Value)
                        oContents.Investor = Convert.ToString(oDbDataReader["Investor"]);
                    if (oDbDataReader["Location"] != DBNull.Value)
                        oContents.Location = Convert.ToString(oDbDataReader["Location"]);
                    if (oDbDataReader["Note"] != DBNull.Value)
                        oContents.Note = Convert.ToString(oDbDataReader["Note"]);
                    if (oDbDataReader["IsSpecial"] != DBNull.Value)
                        oContents.IsSpecial = (Convert.ToBoolean(oDbDataReader["IsSpecial"]) == true) ? "VIP" : "Thường";
                    if (oDbDataReader["UserID"] != DBNull.Value)
                        oContents.UserID = Convert.ToInt32(oDbDataReader["UserID"]);
                    if (oDbDataReader["Duration"] != DBNull.Value)
                        oContents.Duration = Convert.ToString(oDbDataReader["Duration"]);
                    if (oDbDataReader["Content"] != DBNull.Value)
                        oContents.Content = Convert.ToString(oDbDataReader["Content"]);
                    if (oDbDataReader["FullName"] != DBNull.Value)
                        oContents.FullName = Convert.ToString(oDbDataReader["FullName"]);
                    if (oDbDataReader["Position"] != DBNull.Value)
                        oContents.Position = Convert.ToString(oDbDataReader["Position"]);
                    if (oDbDataReader["Mobile"] != DBNull.Value)
                        oContents.Mobile = Convert.ToString(oDbDataReader["Mobile"]);
                    if (oDbDataReader["Mobile"] != DBNull.Value)
                        oContents.Mobile = Convert.ToString(oDbDataReader["Mobile"]);
                    if (oDbDataReader["Email"] != DBNull.Value)
                        oContents.Email = Convert.ToString(oDbDataReader["Email"]);
                    if (oDbDataReader["Description"] != DBNull.Value)
                        oContents.Description = Convert.ToString(oDbDataReader["Description"]);
                    if (oDbDataReader["ContentType"] != DBNull.Value)
                        oContents.ContentType = Convert.ToString(oDbDataReader["ContentType"]);
                    if (oDbDataReader["DistrictID"] != DBNull.Value)
                        oContents.DistrictID = Convert.ToInt32(oDbDataReader["DistrictID"]);
                    if (oDbDataReader["CityId"] != DBNull.Value)
                        oContents.CityId = Convert.ToInt32(oDbDataReader["CityId"]);
                    lstContentss.Add(oContents);
                }
                oDbDataReader.Close();
                return lstContentss;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void getPriceRange(long price, ref long priceMin, ref long priceMax, ref bool isAuction)
        {
            isAuction = false;
            switch (price)
            {
                case 0:
                    priceMin = 0; priceMax = 9223372036854775807;
                    break;
                case 1:
                    priceMin = 0; priceMax = 0;
                    break;
                case 2:
                    priceMin = 0; priceMax = 100000000;
                    break;
                case 3:
                    priceMin = 100000000; priceMax = 250000000;
                    break;
                case 4:
                    priceMin = 250000000; priceMax = 500000000;
                    break;
                case 5:
                    priceMin = 500000000; priceMax = 1000000000;
                    break;
                case 6:
                    priceMin = 1000000000; priceMax = 2500000000;
                    break;
                case 7:
                    priceMin = 2500000000; priceMax = 5000000000;
                    break;
                case 8:
                    priceMin = 5000000000; priceMax = 10000000000;
                    break;
                case 9:
                    priceMin = 10000000000; priceMax = 250000000000;
                    break;
                case 10:
                    priceMin = 250000000000; priceMax = 500000000000;
                    break;
                case 11:
                    priceMin = 500000000000; priceMax = 1000000000000;
                    break;
                case 12:
                    priceMin = 1000000000000; priceMax = 2500000000000;
                    break;
                case 13:
                    priceMin = 2500000000000; priceMax = 5000000000000;
                    break;
                case 14:
                    priceMin = 5000000000000; priceMax = 9223372036854775807;
                    break;
                case 15:
                    priceMin = 0; priceMax = 0;
                    break;
                case 16:
                    priceMin = 0; priceMax = 9223372036854775807;
                    isAuction = true;
                    break;
                default:
                    priceMin = 0; priceMax = 9223372036854775807;
                    break;
            }
        }
        public List<VwContents> GetViewContent()
        {
            try
            {
                List<VwContents> lstContentss = new List<VwContents>();
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECTVw_Contents", CommandType.StoredProcedure);
                DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
                while (oDbDataReader.Read())
                {
                    VwContents oContents = new VwContents();
                    oContents.Id = Convert.ToInt32(oDbDataReader["Id"]);
                    oContents.Title = Convert.ToString(oDbDataReader["Title"]);
                    oContents.Code = Convert.ToString(oDbDataReader["Code"]);
                    oContents.CreatedTime = Convert.ToDateTime(oDbDataReader["CreatedTime"]).ToString("dd/MM/yyyy");
                    if (oDbDataReader["ImgPath"] != DBNull.Value)
                        oContents.ImgPath = Convert.ToString(oDbDataReader["ImgPath"]);
                    if (oDbDataReader["CategoryId"] != DBNull.Value)
                        oContents.CategoryId = Convert.ToInt32(oDbDataReader["CategoryId"]);
                    if (oDbDataReader["Price"] != DBNull.Value)
                        oContents.Price = Convert.ToInt64(oDbDataReader["Price"]);
                    if (oDbDataReader["TradeType"] != DBNull.Value)
                        oContents.TradeType = Convert.ToString(oDbDataReader["TradeType"]);
                    if (oDbDataReader["CityName"] != DBNull.Value)
                        oContents.CityName = Convert.ToString(oDbDataReader["CityName"]);
                    if (oDbDataReader["DistrictName"] != DBNull.Value)
                        oContents.DistrictName = Convert.ToString(oDbDataReader["DistrictName"]);
                    if (oDbDataReader["CategoryName"] != DBNull.Value)
                        oContents.CategoryName = Convert.ToString(oDbDataReader["CategoryName"]);
                    if (oDbDataReader["CompanyName"] != DBNull.Value)
                        oContents.CompanyName = Convert.ToString(oDbDataReader["CompanyName"]);
                    if (oDbDataReader["CompanyAddress"] != DBNull.Value)
                        oContents.CompanyAddress = Convert.ToString(oDbDataReader["CompanyAddress"]);
                    if (oDbDataReader["CompanyTel"] != DBNull.Value)
                        oContents.CompanyTel = Convert.ToString(oDbDataReader["CompanyTel"]);
                    if (oDbDataReader["CompanyFax"] != DBNull.Value)
                        oContents.CompanyFax = Convert.ToString(oDbDataReader["CompanyFax"]);
                    if (oDbDataReader["CompanyEmail"] != DBNull.Value)
                        oContents.CompanyEmail = Convert.ToString(oDbDataReader["CompanyEmail"]);
                    if (oDbDataReader["Website"] != DBNull.Value)
                        oContents.Website = Convert.ToString(oDbDataReader["Website"]);
                    if (oDbDataReader["BusinessOwner"] != DBNull.Value)
                        oContents.BusinessOwner = Convert.ToString(oDbDataReader["BusinessOwner"]);
                    if (oDbDataReader["TaxCode"] != DBNull.Value)
                        oContents.TaxCode = Convert.ToString(oDbDataReader["TaxCode"]);
                    if (oDbDataReader["StartTime"] != DBNull.Value)
                        oContents.StartTime = Convert.ToDateTime(oDbDataReader["StartTime"]).ToString("dd/MM/yyyy");
                    if (oDbDataReader["Status"] != DBNull.Value)
                        oContents.Status = Convert.ToString(oDbDataReader["Status"]);
                    if (oDbDataReader["Revenue"] != DBNull.Value)
                        oContents.Revenue = Convert.ToString(oDbDataReader["Revenue"]);
                    if (oDbDataReader["LastRevenue"] != DBNull.Value)
                        oContents.LastRevenue = Convert.ToString(oDbDataReader["LastRevenue"]);
                    if (oDbDataReader["AnnualProfit"] != DBNull.Value)
                        oContents.AnnualProfit = Convert.ToString(oDbDataReader["AnnualProfit"]);
                    if (oDbDataReader["LastAnualProfit"] != DBNull.Value)
                        oContents.LastAnualProfit = Convert.ToString(oDbDataReader["LastAnualProfit"]);
                    if (oDbDataReader["Assets"] != DBNull.Value)
                        oContents.Assets = Convert.ToString(oDbDataReader["Assets"]);
                    if (oDbDataReader["Reason"] != DBNull.Value)
                        oContents.Reason = Convert.ToString(oDbDataReader["Reason"]);
                    if (oDbDataReader["SaleType"] != DBNull.Value)
                        oContents.SaleType = Convert.ToString(oDbDataReader["SaleType"]);
                    if (oDbDataReader["IsAuction"] != DBNull.Value)
                        oContents.Auction = (Convert.ToBoolean(oDbDataReader["IsAuction"]) == true) ? "Có" : "Không";
                    if (oDbDataReader["ProjectTitle"] != DBNull.Value)
                        oContents.ProjectTitle = Convert.ToString(oDbDataReader["ProjectTitle"]);
                    if (oDbDataReader["Investor"] != DBNull.Value)
                        oContents.Investor = Convert.ToString(oDbDataReader["Investor"]);
                    if (oDbDataReader["Location"] != DBNull.Value)
                        oContents.Location = Convert.ToString(oDbDataReader["Location"]);
                    if (oDbDataReader["Note"] != DBNull.Value)
                        oContents.Note = Convert.ToString(oDbDataReader["Note"]);
                    if (oDbDataReader["IsSpecial"] != DBNull.Value)
                        oContents.IsSpecial = (Convert.ToBoolean(oDbDataReader["IsSpecial"]) == true) ? "VIP" : "Thường";
                    if (oDbDataReader["UserID"] != DBNull.Value)
                        oContents.UserID = Convert.ToInt32(oDbDataReader["UserID"]);
                    if (oDbDataReader["Duration"] != DBNull.Value)
                        oContents.Duration = Convert.ToString(oDbDataReader["Duration"]);
                    if (oDbDataReader["Content"] != DBNull.Value)
                        oContents.Content = Convert.ToString(oDbDataReader["Content"]);
                    if (oDbDataReader["FullName"] != DBNull.Value)
                        oContents.FullName = Convert.ToString(oDbDataReader["FullName"]);
                    if (oDbDataReader["Position"] != DBNull.Value)
                        oContents.Position = Convert.ToString(oDbDataReader["Position"]);
                    if (oDbDataReader["Mobile"] != DBNull.Value)
                        oContents.Mobile = Convert.ToString(oDbDataReader["Mobile"]);
                    if (oDbDataReader["Mobile"] != DBNull.Value)
                        oContents.Mobile = Convert.ToString(oDbDataReader["Mobile"]);
                    if (oDbDataReader["Email"] != DBNull.Value)
                        oContents.Email = Convert.ToString(oDbDataReader["Email"]);
                    if (oDbDataReader["Description"] != DBNull.Value)
                        oContents.Description = Convert.ToString(oDbDataReader["Description"]);
                    if (oDbDataReader["ContentType"] != DBNull.Value)
                        oContents.ContentType = Convert.ToString(oDbDataReader["ContentType"]);
                    if (oDbDataReader["DistrictID"] != DBNull.Value)
                        oContents.DistrictID = Convert.ToInt32(oDbDataReader["DistrictID"]);
                    if (oDbDataReader["CityId"] != DBNull.Value)
                        oContents.CityId = Convert.ToInt32(oDbDataReader["CityId"]);
                    lstContentss.Add(oContents);
                }
                oDbDataReader.Close();
                return lstContentss;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
